package com.huluobo.h3kotlinproject.home

import android.content.Context
import android.content.Intent
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.huluobo.h3kotlinproject.R
import com.huluobo.h3kotlinproject.base.BaseMVVMFragment
import com.huluobo.h3kotlinproject.data.GoodsBean
import com.huluobo.h3kotlinproject.databinding.FragmentHomeBinding
import com.huluobo.h3kotlinproject.detail.DetailActivity
import com.huluobo.h3kotlinproject.detail.DetailViewModel
import com.huluobo.h3kotlinproject.net.Const
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader


class HomeFragment : BaseMVVMFragment<FragmentHomeBinding>() {

    private lateinit var viewModel: HomeViewModel
    private lateinit var goodListAdapter: HomeGoodListAdapter
    private val goodsList = mutableListOf<GoodsBean>()
    private val bannerUrls = mutableListOf<String>()
    private val bannerTitles = mutableListOf<String>()
    private lateinit var navAdapter: NavAdapter
    private val navList = mutableListOf<NavGoodsBean>()

    private var pageIndex = 1
    private var pageSize = 10
    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initData() {
        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]
        viewModel.getHomeGoodsList(0, pageIndex, pageSize)

        viewModel.goodListSuccess.observe(this) {
            if (pageIndex == 1) {
                goodsList.clear()
            }

            dataBinding.homeRefreshLayout.finishRefresh()
            dataBinding.homeRefreshLayout.finishLoadMore()

            goodsList.addAll(it.data)
            goodListAdapter.notifyDataSetChanged()
        }

        viewModel.getHomeBanners()

        viewModel.bannerSuccess.observe(this) {
            bannerUrls.clear()
            bannerTitles.clear()
            it.data.forEach {
                bannerUrls.add(it.imagePath)
                bannerTitles.add(it.desc)
            }
            dataBinding.homeBanner.setImages(bannerUrls)
            dataBinding.homeBanner.setBannerTitles(bannerTitles)
            dataBinding.homeBanner.start()
        }

        viewModel.getNav()
        viewModel.navSuccess.observe(this) {
            navList.clear()
            navList.addAll(it.data)
            navAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        //初始化rv
        dataBinding.homeRv.layoutManager = GridLayoutManager(context, 2)
        goodListAdapter = HomeGoodListAdapter(goodsList)
        dataBinding.homeRv.adapter = goodListAdapter

        goodListAdapter.setOnItemClickListener { _, _, position ->
            //ViewModel如果要实现数据共享他需要根据owner来决定
            //2个Activity之间无法实现数据共享
            //2个Fragment之间,如果处于同一activity之上,才能实现数据共享
            //requireActivity() 找到当前fragment处于的activity
//
//            val detailViewModel = ViewModelProvider(requireActivity())[DetailViewModel::class.java]
//            detailViewModel.setGoodsId(goodsList[position].id)//给viewModel中的LiveData存值

            val intent = Intent(requireActivity(), DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, goodsList[position].id)
            startActivity(intent)
        }
        //初始化下拉刷新控件
        dataBinding.homeRefreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                if (refreshLayout.isLoading) {
                    refreshLayout.finishLoadMore()
                }
                pageIndex = 1
                viewModel.getHomeGoodsList(0, pageIndex, pageSize)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }

                pageIndex += 1
                viewModel.getHomeGoodsList(0, pageIndex, pageSize)
            }
        })

        //初始化banner
        //设置banner风格
        dataBinding.homeBanner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE)
        //设置banner的图片读取器
        dataBinding.homeBanner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                if (context != null && imageView != null) {
                    Glide.with(context).load(path).into(imageView)
                }
            }
        })
        //设置翻页时间
        dataBinding.homeBanner.setDelayTime(1500)
        //设置指示器位置
        dataBinding.homeBanner.setIndicatorGravity(BannerConfig.CENTER)

        //初始化分类控件
        dataBinding.homeNavRv.layoutManager = GridLayoutManager(context, 6)
        navAdapter = NavAdapter(navList)
        dataBinding.homeNavRv.adapter = navAdapter
    }

}